﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OTS.BLL;

public partial class Delivery_DeliveryApproval : INTPage
{
    CommonMethod comHelp = new CommonMethod();
    DeliveryModel model = new DeliveryModel();
    public string chkFlag = "block";
    public string applyTyple = "ApplyApproval";

    #region
    private string _tradingTable;
    public string tradingTable
    {
        get { return _tradingTable; }
        set { _tradingTable = value; }
    }
    public string getTrading()
    {
        return tradingTable;
    }
    #endregion
    #region
    private string _userCode;
    public string userCode
    {
        get { return _userCode; }
        set { _userCode = value; }
    }
    public string getUserCode()
    {
        return userCode;
    }
    #endregion
    #region
    private string _coCode;
    public string coCode
    {
        get { return _coCode; }
        set { _coCode = value; }
    }
    public string getCoCode()
    {
        return coCode;
    }
    #endregion

    protected void Page_Load(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        txtUserCode.Value = Session["UserCode"].ToString();
        txtCoCode.Value = Session["CoCode"].ToString();
        if (!IsPostBack)
        {
            // 页面权限
            comHelp.GetRight(this, new string[] { Session["UserCode"].ToString(), Session["CoCode"].ToString(), 
                "DeliveryApproval" }, null, new string[] { "cmdApproval", "cmdReject", "cmdCancel" }, new int[] { }, CommonModel.loginOut1);
            if (!cmdApproval.Visible)
            { cmdHold.Visible = false; cmdCancel.Visible = false; cmdReject.Visible = false; }
            Initi();
        }
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    public void Initi()
    {
        //GetHead();
        //QueryBind(0);
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
        DdlBind();
    }
    public void GetHead()
    {
        CommonMethod.IsTimeOutUserCode(this, CommonModel.errorTimeOutUrl1);
        Session["headDt"] = DeliveryBLL.GetHead(Session["UserCode"].ToString());
    }
    public DataTable QueryBind(int n)
    {
        StringBuilder strSql = new StringBuilder("");
        if (!ddlState.SelectedValue.Equals("-1"))
        {
            strSql.AppendFormat(" and TDA.TradingState='{0}'  ", ddlState.SelectedValue);
        }
        if (n == 1)
        {
            if (!string.IsNullOrEmpty(txtContractNo.Text))
            {
                strSql.AppendFormat(" and TDA.ContractNoMain like '%{0}%' ", txtContractNo.Text);
            }
            if (!string.IsNullOrEmpty(txtSoNo.Text))
            {
                strSql.AppendFormat(" and TDA.TradingSoNo like '%{0}%'", txtSoNo.Text);
            }
            if (!string.IsNullOrEmpty(txtSaleName.Text))
            {
                strSql.AppendFormat(" and TDA.ContractNoMain in (select distinct ContractNoMain from M_SalesCatalog where SalesName like '%{0}%')", txtSaleName.Text);
            }
            if (!string.IsNullOrEmpty(txtCustomer.Text))
            {
                strSql.AppendFormat(" and TDA.ContractNoMain in (select distinct ContractNoMain from M_Customer where CustomerName like '%{0}%')", txtCustomer.Text);
            }
        }

        return DeliveryBLL.GetDeliveryApproval(strSql.ToString());
    }

    protected void cmdSearch_Click(object sender, EventArgs e)
    {
        //QueryBind(1);
        HidQueryFlag.Value = "1";

        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void cmdReset_Click(object sender, EventArgs e)
    {
        txtDateFrom.Value = "";
        txtDateTo.Value = "";
        txtContractNo.Text = "";
        txtCustomer.Text = "";
        txtSaleName.Text = "";
        txtSoNo.Text = "";
        HidQueryFlag.Value = "0";
        lblTotal.Text = "0";
        txtNewPageIndex.Value = "0";
        lblPageCount.Text = "0";
        //ChangeBind(sender, e);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    public void ChangeBind(object sender,EventArgs e)
    {
        if (Session["ApplyApproval"] != null)
        { tradingTable = Session["ApplyApproval"].ToString(); }
        else { cmdSearch_Click(sender, e); }
    }
    public DataTable getTablesForBuild()
    {       
        if (Session["rowDt"] != null)
        {
            QueryBind(Convert.ToInt16(HidQueryFlag.Value));
        }
        return (DataTable)Session["rowDt"];
    }
    public string TradingTableBuild(DeliveryModel model)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        model.UserCode = Session["UserCode"].ToString();
        model.CoCode = Session["CoCode"].ToString();
        model.ApplyTyple = "ApplyApproval";
        model.ApprovalState = ddlState.SelectedValue.ToString();

        DataTable dt = QueryBind(Convert.ToInt16(HidQueryFlag.Value));
        if (dt!=null && dt.Rows.Count > 0)
        {
            string temp = DeliveryApprovalBuildBLL.DeliveryApprovalBuild(dt, model);
            SetPage(dt, model);

            Session["ApplyApproval"] = temp;
            return temp;
        }
        return "Can not get data！";
    }
    // 换页设置
    public void SetPage(DataTable dt, DeliveryModel model)
    {
        int toaltNum = dt.Rows.Count;
        int pagesize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        this.lblTotal.Text = toaltNum.ToString();
        int pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        int pageCount = toaltNum / pagesize;

        if (pageCount * pagesize == toaltNum) { }
        else { pageCount = pageCount + 1; }

        txtNewPageIndex.Value = pageIndex.ToString();

        this.lblPageCount.Text = pageCount.ToString();

        if (pageIndex < pageCount)
        {
            if (pageIndex == 1)
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = true;
                lbtLastPage.Enabled = true;
            }
        }
        else
        {
            if (pageIndex > 1)
            {
                lbtFirstPage.Enabled = true;
                lbtPreviousPage.Enabled = true;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = true;
            }
            else
            {
                lbtFirstPage.Enabled = false;
                lbtPreviousPage.Enabled = false;
                lbtNextPage.Enabled = false;
                lbtLastPage.Enabled = false;
                btnGo.Enabled = false;
            }
        }
    }

    #region 换页
    // 跳页
    protected void btnGo_Click(object sender, EventArgs e)
    {
        string temp = tradingTable;
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(txtNewPageIndex.Value.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        if (!(model.pageIndex == model.pageNextIndex))
        {
            model.chkFlag = chkFlag;
            temp = TradingTableBuild(model);
        }
        tradingTable = temp;
    }
    //第一页
    protected void lbtFirstPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    // 上一页
    protected void lbtPreviousPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) - 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    // 下一页
    protected void lbtNextPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = Convert.ToInt16(lblPageIndex.Text.ToString()) + 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);

    }
    // 最后一页
    protected void lbtLastPage_Click(object sender, EventArgs e)
    {
        model.pageIndex = Convert.ToInt16(lblPageCount.Text.ToString()) - 1;
        model.pageNextIndex = Convert.ToInt16(lblPageCount.Text.ToString());
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        tradingTable = TradingTableBuild(model);
    }
    protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
    {
        CommonMethod.IsTimeOutUserCodeAndCoCode(this, CommonModel.errorTimeOutUrl1);
        model.pageIndex = Convert.ToInt16(lblPageIndex.Text.ToString());
        model.pageNextIndex = 1;
        model.pageSize = Convert.ToInt16(ddlPageSize.SelectedItem.Text.ToString());
        lblPageIndex.Text = model.pageNextIndex.ToString();
        //绑定内容
        model.chkFlag = chkFlag;
        model.UserCode = Session["UserCode"].ToString();
        tradingTable = TradingTableBuild(model);
    }
    #endregion

    public void DdlBind()
    {
        ListItem item = new ListItem("请选择(Select Please)", "-1");
        DataTable[] dt = DeliveryBLL.GetddlForApprovalQuery();
        ddlContractNo.DataSource = dt[0];
        ddlContractNo.DataValueField = "ContractNoMain";
        ddlContractNo.DataTextField = "ContractNoMain";
        ddlContractNo.DataBind();
        ddlContractNo.Items.Insert(0, item);
        txtContractNo.Text = "";

        ddlContractNoCustomer.DataSource = dt[6];
        ddlContractNoCustomer.DataValueField = "ContractNoCust";
        ddlContractNoCustomer.DataTextField = "ContractNoCust";
        ddlContractNoCustomer.DataBind();
        ddlContractNoCustomer.Items.Insert(0, item);
        txtContractNoCustomer.Text = "";

        ddlSoNo.DataSource = dt[1];
        ddlSoNo.DataValueField = "ContractNoMain";
        ddlSoNo.DataTextField = "TradingSoNo";
        ddlSoNo.DataBind();
        ddlSoNo.Items.Insert(0, item);
        txtSoNo.Text = "";

        ddlSaleName.DataSource = dt[2];
        ddlSaleName.DataValueField = "SalesName";
        ddlSaleName.DataTextField = "SalesName";
        ddlSaleName.DataBind();
        ddlSaleName.Items.Insert(0, item);
        txtSaleName.Text = "";

        ddlCustomer.DataSource = dt[3];
        ddlCustomer.DataValueField = "CustomerName";
        ddlCustomer.DataTextField = "CustomerName";
        ddlCustomer.DataBind();
        ddlCustomer.Items.Insert(0, item);
        txtCustomer.Text = "";
    }
    protected void ddlContractNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        string contractNo = ddlContractNo.SelectedValue.ToString();
        if (!contractNo.Equals("-1"))
        { txtContractNo.Text = contractNo; }
        else
        { txtContractNo.Text = ""; }
        ChangeBind(sender, e);
        //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlContractNo','ddlContractNo', 'txtContractNo','txtContractNo')", this);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void ddlSoNo_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSoNo.SelectedValue.Equals("-1"))
        { txtSoNo.Text = ddlSoNo.SelectedItem.Text; }
        else
        { txtSoNo.Text = ""; }
        ChangeBind(sender, e);
        //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlSoNo','ddlSoNo', 'txtSoNo','txtSoNo')", this);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void ddlCustomer_SelectedIndexChanged(object sender, EventArgs e)
    {        
        if (!ddlCustomer.SelectedValue.Equals("-1"))
        { txtCustomer.Text = ddlCustomer.SelectedItem.Text; }
        else
        { txtCustomer.Text = ""; }
        ChangeBind(sender, e);
        //CommonMethod.regAlertMessageScript(UpdHead, "showHidden('ddlCustomer','ddlCustomer', 'txtCustomer','txtCustomer')", this);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void ddlSaleName_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (!ddlSaleName.SelectedValue.Equals("-1"))
        { txtSaleName.Text = ddlSaleName.SelectedItem.Text; }
        else
        { txtSaleName.Text = ""; }
        ChangeBind(sender, e);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
    protected void ddlContractNoCustomer_TextChanged(object sender, EventArgs e)
    {
        if (!ddlContractNoCustomer.SelectedValue.Equals("-1"))
        { txtContractNoCustomer.Text = ddlContractNoCustomer.SelectedItem.Text; }
        else
        { txtContractNoCustomer.Text = ""; }
        ChangeBind(sender, e);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }

    protected void ddlState_TextChanged(object sender, EventArgs e)
    {
        cmdApproval.Disabled = true;
        cmdCancel.Disabled = true;
        cmdReject.Disabled = true;
        cmdHold.Disabled = true;

        if (ddlState.SelectedValue.Equals("Approval") || ddlState.SelectedValue.Equals("Hold"))
        { cmdApproval.Disabled = false; cmdReject.Disabled = false; cmdHold.Disabled = false; }
        else if (ddlState.SelectedValue.Equals("Approved"))
        { cmdCancel.Disabled = false; }
        cmdSearch_Click(sender, e);
        CommonMethod.regAlertMessageScript(UpdHead, "deliveryOnloadApply();", this);
    }
}